var sys_time = {};
var format = {};
var LOGIN_OPEN = '0';

google.setOnLoadCallback(function() {	
	// jquery function login
	(function($)
	{
		 $.fn.login = function() { 
			$('#loading_image_signin').show();
			$.post(BASE_URI+MODULE+'/user/login',{username:$('#username').val(),password:$('#password').val()},function(res){
				$('#loading_image_signin').hide();
				if(res == 'ok'){
					reload();
				}else{
					jQuery('#hiddenElement').html(res);
					jQuery('#hiddenElement').dialog(
						{
							 width: 350,
							 resizable: false,
							 height:200 
							// title: '' 
						}
					);
				}
			});
		 };  
	})(jQuery);
	
	//countdown time
	(function($)
	{
		 $.fn.countdown = function(div_id, time,formattext) { 
			format.f =formattext;
			eval( "sys_time."+div_id+"="+time);
			calcountdown();
			window.setInterval("calcountdown()",1000);
		 };  
	})(jQuery);
	
	//toggle div
	(function($)
	{
		 $.fn.divtoggle = function(div_id) { 
			if( $('#'+div_id).hasClass("hidden") ){
				$('#'+div_id).removeClass("hidden");
				$(this).addClass('uncollapse');
			}else{
				$('#'+div_id).addClass("hidden");
				$(this).removeClass('uncollapse');
			}
		 };  
	})(jQuery);
	
	//direct bet manual
	(function($){
		$.fn.place_direct_bet_manual = function(cat_id){
			$stake = $('#direct_stake_'+cat_id).attr('value');
			$points = $('#direct_win_'+cat_id).attr('value');
			$type = $('#direct_event_type_'+cat_id).attr('value');
			
			if($type =='range'){
				$from = $('#range_from_'+cat_id).attr('value');
				$to = $('#range_to_'+cat_id).attr('value');
				if(!isNaturalNumber($from)){
					$('#range_from_'+cat_id).addClass('error');
					return;
				}else{
					$('#range_from_'+cat_id).removeClass('error');
				}	
				if(!isNaturalNumber($to)){
					$('#range_to_'+cat_id).addClass('error');
					return;
				}else{
					$('#range_to_'+cat_id).removeClass('error');
				}
				$value = $from+'{[|SEPSTR|]}'+$to;				
			}else{
				$array_value = getValueInputArray('box_'+cat_id);
				$value = $array_value? $array_value.join('{[|SEPSTR|]}') : '';
			}
			if(PRE_QUESTION){
				jQuery('#hiddenElement').html(PRE_QUESTION);
				jQuery( "#hiddenElement" ).dialog({
					resizable: false,
					height:200,
					width: 350,
					//modal: true,
					buttons: {
						"Ok": function() {
							$( this ).dialog( "close" );
							$.post(BASE_URI+MODULE+'/async/direct_bet_manual',{cat_id:cat_id,stake:$stake,points:$points,value:$value},function(res){
								var result = jQuery.parseJSON(res);
								
								jQuery('#hiddenElement').html(result.msg);
								jQuery('#hiddenElement').dialog(
									{
										 width: 350,
										 resizable: false,
										 height:200 ,
										 beforeClose: function(event, ui) { 
											if(result.status == 'ok'){
												clearManualBetValue();
												reload();
											}
										 },
										 buttons: {
              					"Ok": function() {
              						$( this ).dialog( "close" );
              					}
              				}
									}
								);
							});
						},
						Cancel: function() {
							$( this ).dialog( "close" );
						}
					}
				});
			}else{
				$.post(BASE_URI+MODULE+'/async/direct_bet_manual',{cat_id:cat_id,stake:$stake,points:$points,value:$value},function(res){
					var result = jQuery.parseJSON(res);
					
					jQuery('#hiddenElement').html(result.msg);
					jQuery('#hiddenElement').dialog(
						{
							 width: 350,
							 resizable: false,
							 height:200 ,
							 beforeClose: function(event, ui) { 
								if(result.status == 'ok'){
									clearManualBetValue();
									reload();
								}
							 },
              buttons: {
        					"Ok": function() {
        						$( this ).dialog( "close" );
        					}
        				}
						}
					);
				});
			}
		}
	})(jQuery);
	
	//direct place bet	
	(function($)
	{
		$.fn.place_direct_bet = function(event_id) { 
			$stake = $('#direct_stake_'+event_id).attr('value');
			$points = $('#direct_win_'+event_id).attr('value');
			if(PRE_QUESTION){
				jQuery('#hiddenElement').html(PRE_QUESTION);
				jQuery( "#hiddenElement" ).dialog({
					resizable: false,
					height:200,
					width: 350,
					//modal: true,
					buttons: {
						"Ok": function() {
							$( this ).dialog( "close" );
							$.post(BASE_URI+MODULE+'/async/direct_bet',{event_id:event_id,stake:$stake,points:$points},function(res){
								var result = jQuery.parseJSON(res);
								
								jQuery('#hiddenElement').html(result.msg);
								jQuery('#hiddenElement').dialog(
									{
										 width: 350,
										 resizable: false,
										 height:200 ,
										 beforeClose: function(event, ui) { 
											if(result.status == 'ok'){
												reload();
											}
										 },
										  buttons: {
              					"Ok": function() {
              						$( this ).dialog( "close" );
              					}
              				}
									}
								);
							});
						},
						Cancel: function() {
							$( this ).dialog( "close" );
						}
					}
				});
			}else{
				$.post(BASE_URI+MODULE+'/async/direct_bet',{event_id:event_id,stake:$stake,points:$points},function(res){
					var result = jQuery.parseJSON(res);
					
					jQuery('#hiddenElement').html(result.msg);
					jQuery('#hiddenElement').dialog(
						{
							 width: 350,
							 resizable: false,
							 height:200 ,
							 beforeClose: function(event, ui) { 
								if(result.status == 'ok'){
									reload();
								}
							 },
                buttons: {
        					"Ok": function() {
        						$( this ).dialog( "close" );
        					}
        				}
						}
					);
				});
			}	
		}; 
			
	})(jQuery);
	//clickbet
	(function($)
	{
		 $.fn.clickbet = function(event_id) { 
			$stake = $('#direct_stake_'+event_id).attr('value');
			$points = $('#direct_win_'+event_id).attr('value');
			
			$.post(BASE_URI+MODULE+'/async/setbetting',{event_id:event_id,stake:$stake,points:$points},function(res){
				$('.direct_bet').addClass('hidden');
				opening_event.id = 0;
				
				var result = jQuery.parseJSON(res);
				if(result.status == 'ok'){
					$('#title_sys_plugin').remove();
					$('#bet').append(result.dom);
				}else{
					jQuery('#hiddenElement').html(result.msg);
					jQuery('#hiddenElement').dialog(
						{
							 width: 350,
							 resizable: false,
							 height:200 ,
              buttons: {
          					"Ok": function() {
          						$( this ).dialog( "close" );
          					}
          				}
							// title: '' 
						}
					);
				}
			});
			 
		 };  
	})(jQuery);
	
	//click to open direct bet
	(function($)
	{
		 $.fn.click2open = function(event_id) { 
			$('.direct_bet').addClass('hidden');
			if(!opening_event.id){
				$('#direct_bet_'+event_id).removeClass('hidden');
				opening_event.id = event_id;
				opening_event.html = $('#direct_bet_'+event_id).html();
			}else{
				$('#direct_bet_'+event_id).addClass('hidden');
				opening_event.id = 0;
			}
		 };  
	})(jQuery);
	
	//delete bet
	(function($)
	{
		 $.fn.deletebet = function(div_id) { 
			$('#event_'+div_id).remove();
		 };  
	})(jQuery);
	
	//convertion
	(function($)
	{
		 $.fn.convertion = function(div_show, point) { 
			 if(!isNaturalNumber(point)){
				$('#point').addClass('error');
				$('#'+div_show).attr('value','');
				return;
			 }else{
				$('#point').removeClass('error');
			 }	
			 var coin = Math.floor( point*sysconvertion.coin/sysconvertion.point );
			 $('#'+div_show).attr('value',coin);
		 };  
	})(jQuery);
	
	//calculate range denote
	(function($){
		$.fn.calculateRangeDenote = function(cat_id, fromValue){
			if(!isNaturalNumber(fromValue)){
				$('#range_from_'+cat_id).addClass('error');
				return;
			}else{
				$('#range_from_'+cat_id).removeClass('error');
			}
			$denote = $('#direct_event_denote_'+cat_id).attr('value');
			$toValue = parseInt( parseInt(fromValue) + parseInt($denote) );
			$('#range_to_'+cat_id).attr('value',$toValue);
			
			storageManualBetValue();
		};
		
		$.fn.storeValueInputBoxes = function(cat_id, value){
			storageManualBetValue();
		};
	})(jQuery);
	
	//calculate win points get
	(function($)
	{
		 $.fn.calculateWinPointsGet = function(uid, stake) { 
			 
			 if(!isNaturalNumber(stake)){
				$('#stake_'+uid).addClass('error');
				$('#win_'+uid).attr('value','');
				return;
			 }else{
				$('#stake_'+uid).removeClass('error');
			 }	
			 var odds = $('#event_'+uid).attr('rel');
			 
			 var points = parseFloat( stake*odds ).toFixed(2);
			// var points = Math.floor( stake*odds );
			 $('#win_'+uid).attr('value',points);
		 };  
	})(jQuery);
	
	//calculate direct win points get
	(function($)
	{
		 $.fn.calculateWinPointsGet_direct = function(uid, stake) { 
			 
			 if(!isNaturalNumber(stake)){
				$('#direct_stake_'+uid).addClass('error');
				$('#direct_win_'+uid).attr('value','');
				return;
			 }else{
				$('#direct_stake_'+uid).removeClass('error');
			 }	
			 var odds = $('#direct_event_odds_'+uid).attr('value');
			 var points = parseFloat( stake*odds ).toFixed(2);
			 //var points = Math.floor( stake*odds );
			 $('#direct_win_'+uid).attr('value',points);
			 $('#direct_stake_'+uid).attr('value',stake);
		 };  
	})(jQuery);
	
	
});

function calcountdown(){	
	var t= 0; 
	for(var i in sys_time){
		 var element = i;
		 var tmp = sys_time[i];
 
		 var days = parseInt( tmp/86400 );
		 var hours = parseInt( (tmp- days*86400)/3600 );
		 var minutes = parseInt( (tmp- (days*86400+hours*3600))/60 );
		 var seconds = parseInt (tmp- (days*86400+hours*3600+minutes*60));
		 var str = "<div class='clock'>";
		 if(days >= 0){
			if(days<= 9)
				days = '0'+days+'';
			str = "<div class='item'><span class='number'>"+days+"</span><span class='text'>d</span></div>";
		 }
		 if(hours >= 0){
			if(hours<= 9)
				hours = '0'+hours+'';
			str += "<div class='item'><span class='number'>"+hours+"</span><span class='text'>h</span></div>";
		 }
		 if(minutes >= 0){
			if(minutes<= 9)
				minutes = '0'+minutes+'';
			str += "<div class='item'><span class='number'>"+minutes+"</span><span class='text'>m</span></div>";
		 }
		 if(seconds >= 0){
			if(seconds<= 9)
				seconds = '0'+seconds+'';
			str += "<div class='item'><span class='number'>"+seconds+"</span><span class='text'>s</span></div>";
		 }
		 str += "</div>";
		 
		 if(format.f == 'lower')
			$("#"+element).html( str );
		 else
			$("#"+element).html( str );//.toUpperCase()
		 t++;
		 if(tmp < 0){
			//return;
			reload();
		 }
	}
	 
	for(var j in sys_time){
		sys_time[j] = sys_time[j] - 1/t;
	}		
}

function storageManualBetValue(){
	$('input.boxes').each(function(i,o){
		$id = $(this).attr('id');
		$value = $('#'+$id).attr('value');
		if($value){
			eval("manual_bet."+$id+"='"+$value+"'");
		}
	});
}

function restoreManualBetValue(){
	for(var id in manual_bet){
		$value = eval("manual_bet."+id);
		$('input#'+id).attr('value',$value);
	}
}

function clearManualBetValue(){
	for(var id in manual_bet){
		$('input#'+id).attr('value','');
	}
	manual_bet = {};
	opening_event.id = 0;
}